﻿Option Strict On
Option Explicit On
Option Infer On

Imports System
Imports System.Collections.Generic
Imports System.AddIn
Imports System.ComponentModel
Imports System.Xml.Linq
Imports System.Xml.XPath
Imports GenDotNet.Task.Common

<AddIn("LinqGeneratorTest", _
    Version:="1.0.0.0")> _
Public Class LinqGeneratorTest
   Inherits LinqGeneratorAddInView.LinqGeneratorAddInBase

   Private xDocument As XDocument

   Public Overrides Function SetXmlDocument(ByVal inputXml As String) As String
      xDocument = xDocument.Parse(inputXml, LoadOptions.PreserveWhitespace)
      Dim ret = <Root>
                   <Objects>
                      <%= From obj In xDocument...<Object> Select <Object Name=<%= obj.@Name %>/> %>
                   </Objects>
                   <Apis>
                      <%= From api In xDocument...<Api> Select <Api Name=<%= api.@Name %>/> %>
                   </Apis>
                   <Tables>
                      <%= From table In xDocument...<Table> Select <Table Name=<%= table.@Name %>/> %>
                   </Tables>
                   <StoredProcs>
                      <%= From sp In xDocument...<StoredProc> Select <StoredProc Name=<%= sp.@Name %>/> %>
                   </StoredProcs>
                </Root>.ToString()
      Return ret
   End Function

   Public Overrides Sub ClearXmlDocument()
      xDocument = Nothing
   End Sub

   Public Overrides Function GenerateForObject( _
         ByVal objectName As String, _
         ByVal assemblyFileName As String, _
         ByVal templateName As String, _
         ByRef fileName As String) _
         As String
      Console.WriteLine("Loaded Secondary app domain")
      Dim asm As Reflection.Assembly = Nothing
      If Not String.IsNullOrEmpty(assemblyFileName) Then
         asm = Reflection.Assembly.LoadFrom(assemblyFileName)
         Dim type As Type = Nothing
         If asm IsNot Nothing And Not String.IsNullOrEmpty(templateName) Then
            type = GetSpecifiedType(asm, templateName)
            If type IsNot Nothing Then
               Dim obj = Activator.CreateInstance(type)
               Dim element As XElement = (From x In xDocument...<Api> Where x.@Name = objectName).FirstOrDefault
               If element IsNot Nothing Then
                  Dim template = TryCast(obj, ITemplate)
                  If template IsNot Nothing Then
                     Return template.Generate(element, fileName)
                  End If
               End If
            End If
         End If
      End If


      Return Nothing
   End Function

   Private Shared Function GetSpecifiedType( _
            ByVal asm As Reflection.Assembly, _
            ByVal name As String) _
            As System.Type
      Dim type As System.Type = Nothing
      ' This is tempoary to test a fix

      Try
         Dim types = asm.GetTypes()
      Catch ex As Exception
         Console.WriteLine(ex)
      End Try

      ' Kind of ugly but it works
      For Each testType As System.Type In asm.GetTypes
         If testType.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase) Then
            type = testType
            Exit For
         End If
      Next
      Return type
   End Function

   Public Overrides Function GenerateForStoredProc( _
         ByVal storedProcName As String, _
         ByVal assemblyFileName As String, _
         ByVal templateName As String, _
         ByRef fileName As String) _
         As String

   End Function

   Public Overrides Function GenerateForDataTable( _
         ByVal dataTableName As String, _
         ByVal assemblyFileName As String, _
         ByVal templateName As String, _
         ByRef fileName As String) _
         As String

   End Function

End Class
